# 将登录页面封装成类
# 在登录的测试模块中，调用该类
from selenium import webdriver
from selenium.webdriver.common.by import By
class PageLogin:
    def __init__(self):
        self.driver = webdriver.Firefox() # 获取浏览器驱动对象
        self.driver.maximize_window() # 最大化浏览器窗口
        self.driver.implicitly_wait(20) # 设置隐式等待时间
        self.driver.get('http://192.168.88.100:8000/admin') # 打开登录页面
    # 定位用户名输入值
    def page_input_username(self, username):
        el = self.driver.find_element(By.ID, 'id_username')
        el.clear() # 清空输入框
        el.send_keys(username)
    # 定位密码输入框
    def page_input_password(self, password):
        self.driver.find_element(By.ID, 'id_password').send_keys(password)
    # 点击登录按钮
    def page_click_login_button(self):
        self.driver.find_element(By.CSS_SELECTOR, '[value="登录"]').click()
    # 组装登录功能
    def page_login(self, username, password):
        self.page_input_username(username)
        self.page_input_password(password)
        self.page_click_login_button()
    # 获取登录的结果
    def page_get_login_result(self):
        return self.driver.find_element(By.CSS_SELECTOR, '.errornote').text
    # 关闭浏览器驱动
    def page_close(self):
        self.driver.quit()

if __name__ == '__main__':
    # 实例化登录页面对象
    login = PageLogin()
    login.page_login('admin', '888999')
    msg = login.page_get_login_result()
    login.page_close()

